package com.nacos.controller;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.nacos.concifg.DataBase;
import com.nacos.concifg.DataBaseConfig;
import com.nacos.concifg.UserConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
//@RefreshScope
public class ConfigController {


    @NacosValue("${test.name}")
    private String name;

    @Value("${test:age}")
    private String age;

    @NacosValue("${test:test_id}")
    private String test_id;


    @Autowired
    UserConfig userConfig;

    // nacos 调用
    @GetMapping("/user")
    public String getUserInfo() {
        return "nacos 调用: User Name: " + name + ",Test_id:" + test_id + ", Age: " + age;
    }

    // 本地bean调用
    @GetMapping("/user1")
    public String getUserInfo1() {
        return "本地bean调用: User Name: " + userConfig.getName() + ",Test_id: " + userConfig.getTest_id() + " , Age: " + userConfig.getAge();
    }

    @Autowired
    DataBaseConfig dataBaseConfig;

    // JsonObject 注入对象中
    @GetMapping("/user2")
    public String getUser() {
        DataBase dataBase = dataBaseConfig.getDataBase();
        return "代用json，写入配置 : JsonObject: " + dataBase.getDataBase();
    }
}